package org.luckyjourney.service.agent.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.luckyjourney.entity.File;
import org.luckyjourney.entity.agent.AgentChat;
import org.luckyjourney.entity.agent.AgentChatTable;
import org.luckyjourney.entity.vo.AgentPageVo;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.mapper.agent.AgentChatMapper;
import org.luckyjourney.mapper.agent.AgentChatTableMapper;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.agent.AgentChatTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 86155
* @description 针对表【agent_chat_table(智能体对话表)】的数据库操作Service实现
* @createDate 2024-03-27 10:45:45
*/
@Service
public class AgentChatTableServiceImpl extends ServiceImpl<AgentChatTableMapper, AgentChatTable>
    implements AgentChatTableService {

    @Autowired
    private AgentChatMapper agentChatMapper;

    @Autowired
    private AgentChatTableMapper agentChatTableMapper;

    @Autowired
    private FileService fileService;

    @Override
    public IPage<AgentChat> listAll(int agent_chat_id, AgentPageVo agentPageVo) {
        Page<AgentChat> page = new Page<>(agentPageVo.getPage(),agentPageVo.getSize());
        Long userId = UserHolder.get();
        LambdaQueryWrapper<AgentChat> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AgentChat::getUserId,userId);
        wrapper.eq(AgentChat::getAgentChatId,agent_chat_id);
        //根据时间倒叙排序
        wrapper.orderByDesc(AgentChat::getCreateTime);
        IPage<AgentChat> agentChatIPage = agentChatMapper.selectPage(page, wrapper);
//        List<AgentChat> records = agentChatIPage.getRecords();
        //将records里面的信息按照顺序排序
//        records.sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()));
        return agentChatIPage;
    }

    @Override
    public AgentChatTable getAgentChatByid(Integer agent_chat_id) {
        AgentChatTable agentChatTable = agentChatTableMapper.selectById(agent_chat_id);
        //将头像照片赋值进去
            if(!StringUtils.isEmpty(agentChatTable.getAgentProfile()) && !ObjectUtils.isEmpty(agentChatTable)){
                Long avatar = Long.valueOf(agentChatTable.getAgentProfile());
                File fileTrustUrl = fileService.getFileTrustUrl(avatar);
                if (!ObjectUtils.isEmpty(fileTrustUrl)){
                    agentChatTable.setAgentProfile(fileTrustUrl.getFileKey());
                }
            }
        //用户将智能体添加到对话的数量
         agentChatTable.setChatsCount(agentChatTableMapper.selectCount(new LambdaQueryWrapper<AgentChatTable>().eq(AgentChatTable::getAgentId, agentChatTable.getAgentId())));
        return agentChatTable;
    }

    @Override
    public String isTopAndUpTop(Integer agent_chat_id) {
        AgentChatTable agentChatTable = agentChatTableMapper.selectById(agent_chat_id);
        if (ObjectUtils.isEmpty(agentChatTable)){
            return "未找到该智能体对话";
        }
        agentChatTable.setIsTop(!agentChatTable.getIsTop());
        agentChatTableMapper.updateById(agentChatTable);
       return agentChatTable.getIsTop()?"置顶成功":"取消置顶成功";
    }


}




